home *** CD-ROM | disk | FTP | other *** search
/ STraTOS 1997 April & May / STraTOS 1 - 1997 April & May.iso / CD01 / PRGMANIA / BFED.10 / KEYS.C < prev    next >
Encoding:
C/C++ Source or Header  |  1997-01-27  |  14.0 KB  |  680 lines

  1. /*
  2.     file: keys.c
  3.     utility:
  4.     date: 1989
  5.     author: Jim Charlton
  6.     modifications:
  7.         1996: C. Moreau: 
  8.     comments: 
  9. */
  10. #include <ctype.h>
  11. #include <stdio.h>
  12. #include <string.h>
  13.  
  14. #ifdef __PUREC__ 
  15. #include <aes.h>
  16. #include <tos.h>
  17. #include <vdi.h>
  18. #include <compend.h>
  19. #else
  20. #include <aesbind.h>
  21. #include <osbind.h>
  22. #include <vdibind.h>
  23. #endif
  24.  
  25. #include "e:\proging\c\libs\malib\alert.h"
  26.  
  27. #include "dialog.h"
  28. #include "edit.h"
  29. #include "init.h"
  30. #include "keys.h"
  31. #include "menu.h"
  32. #include "onepage.h"
  33. #include "send.h"
  34. #include "wind.h"
  35.  
  36. #include "bfed_rsc.h"
  37.  
  38. /*
  39.     globals vars
  40. */
  41. int keycode;
  42.  
  43. /*
  44.     locals vars 
  45. */
  46. static long saveb;
  47. static KEYTAB *kt;
  48.  
  49. /*
  50.     locals functions
  51. */
  52. static void ctrl_keys(int keycode, int keymods);
  53. static void cmd_keys(int keycode, int keymods);
  54. static void cur_up(windowptr    thewin);
  55. static void cur_down(windowptr    thewin);
  56. static void cur_right(windowptr    thewin);
  57. static void cur_left(windowptr    thewin);
  58. static void pag_up(windowptr thewin);
  59. static void pag_down(windowptr thewin);
  60. static void pag_home(windowptr thewin);
  61. static void pag_end(windowptr thewin);
  62. static int hex_chk(windowptr thewin,char    ch);
  63.  
  64. /*
  65.     name: do_kbd
  66.     utility:  
  67.     comment: 
  68.     parameters:
  69.     return:
  70.     date: 1989
  71.     author: Jim Charlton
  72.     modifications:
  73.         1995: C.Moreau: 
  74. */
  75. void do_kbd(int keycode, int keymods)
  76. {    
  77.     if (keymods & K_CTRL)    /* ctrl key is pressed - command mode */
  78.     {
  79.         ctrl_keys(keycode, keymods);
  80.     }
  81.     else if (thefrontwin && !thefrontwin->form) /* window open & not form */
  82.     {
  83.         if (thefrontwin->markson)
  84.             do_menu(EDIT,CLEAR);
  85.  
  86.         if (thefrontwin->icount) /* in the middle of a hex num input */
  87.         {
  88.             if (thefrontwin->prot)
  89.             {
  90.                 rsc_alert(PROTECTED);
  91.             }
  92.             else
  93.             {
  94.                 thefrontwin->input[thefrontwin->icount++] = keycode & 0xFF;
  95.                 ins_rpl(thefrontwin);
  96.             }
  97.         }
  98.         else    /* a command keys is pressed */
  99.         {
  100.             cmd_keys(keycode, keymods);
  101.         }
  102.     }
  103.  
  104.     window_info(thefrontwin);
  105. }
  106.  
  107. /*
  108.     name: cmd_kbd
  109.     utility:  
  110.     comment: 
  111.     parameters:
  112.     return:
  113.     date: 1995
  114.     author: C.Moreau
  115.     modifications:
  116. */
  117. static void cmd_keys(int keycode, int keymods)
  118. {
  119.     
  120.     {
  121.         saveb = getbyte(thefrontwin, thefrontwin->position);
  122.                 /* store the current undirtied byte    */
  123.                 /* then check for special keys        */
  124.     
  125.         switch (keycode)
  126.         {
  127.             case 0x5200:    /* insert */
  128.                 ins ^= TRUE;
  129.                 update_menu();
  130.                 break;
  131.             case 0x4d00:        /* arrow right */
  132.                 cur_right(thefrontwin);
  133.                 break;
  134.             case 0x5000:        /* arrow down */
  135.                 cur_down(thefrontwin);
  136.                 break;
  137.             case 0x4800:        /* arrow up */
  138.                  cur_up(thefrontwin);
  139.                 break;
  140.             case 0x4b00:        /* arrow left */
  141.                 cur_left(thefrontwin);
  142.                 break;
  143.             case 0x4838:        /* shift arrow up */
  144.                 pag_up(thefrontwin);
  145.                 break;
  146.             case 0x5032:        /* shift arrow down */
  147.                 pag_down(thefrontwin);
  148.                 break;
  149.             case 0x4700:        /* Home */
  150.                 pag_home(thefrontwin);
  151.                 break;
  152.             case 0x4737:        /* shift Home */
  153.                 pag_end(thefrontwin);
  154.                 break;
  155.             case 0x537f:        /* Delete */
  156.                 if(thefrontwin->prot)
  157.                     rsc_alert(PROTECTED);
  158.                 else
  159.                 {
  160.                     delete_one(thefrontwin, thefrontwin->position, 1);
  161.                     if (repos(thefrontwin))
  162.                         partial = FALSE;
  163.                     else
  164.                         partial = TRUE;
  165.                     send_redraw(thefrontwin);
  166.                 }
  167.                 break;
  168.             case 0x0e08:        /* Backspac */
  169.                 if(thefrontwin->position-1 >= 0)
  170.                 {
  171.                     if(thefrontwin->prot)
  172.                         rsc_alert(PROTECTED);
  173.                     else
  174.                     {
  175.                         delete_one(thefrontwin, --thefrontwin->position, 1);
  176.                         if (repos(thefrontwin))
  177.                             partial = FALSE;
  178.                         else
  179.                             partial = TRUE;
  180.                          send_redraw(thefrontwin);
  181.     /*                    one_page(thefrontwin); */
  182.                     }
  183.                 }
  184.                 break;
  185.             default:
  186.                 if (!thefrontwin->prot)        /* read-only protection not on  */
  187.                 {
  188.                     if (inhex)    /* handle hex input */
  189.                     {
  190.                         thefrontwin->input[thefrontwin->icount++] = keycode & 0xFF;
  191.                         ins_rpl(thefrontwin);
  192.                     }
  193.                     else     /* handle ascii input    */
  194.                         if ( (thefrontwin->position==thefrontwin->flen-1) && !ins) /* in replace mode on terminal dummy byte */
  195.                         {
  196.                             rsc_alert(LAST_BYTE2);                
  197.                             thefrontwin->icount = 0;
  198.                         }
  199.                         else if (keycode & 0xff)    
  200.                         {
  201.                             char     inbuf[8];
  202.                             long    lnum;
  203.     
  204.                             sprintf(inbuf, "%x", (keycode & 0xff));
  205.                              inbuf[2] = 0;
  206.                              lnum = (long)keycode & 0xff;
  207.                             enter_it(thefrontwin,inbuf,lnum);
  208.                         }
  209.                 }
  210.                 else
  211.                     rsc_alert(PROTECTED);                
  212.         }
  213.     }
  214. }
  215.  
  216. /*
  217.     name: ctrl_kbd
  218.     utility:  
  219.     comment: 
  220.     parameters:
  221.     return:
  222.     date: 1995
  223.     author: C.Moreau
  224.     modifications:
  225. */
  226. static void ctrl_keys(int keycode, int keymods)
  227. {
  228.         /* switch on ascii key code without shift */
  229.      switch ((char)toupper(kt->unshift[(char)(keycode >> 8)]))
  230.     {
  231.         case 'A':    
  232.             do_menu(EDIT,SELECT_ALL);
  233.             break;
  234.         case 'C':    
  235.             do_menu(EDIT,COPY);
  236.             break;
  237.         case 'D':    
  238.             do_menu(EDIT,CLEAR);
  239.             break;
  240.         case 'F':    
  241.             do_menu(SEARCH,FIND);
  242.             break;
  243.         case 'L':        /* Go position */
  244.             do_menu(SEARCH,GO_POS);
  245.             break;
  246.         case 'M':        /* Mark start/end */
  247.             if ( (keymods & K_RSHIFT) || (keymods & K_LSHIFT) )
  248.                 do_menu(EDIT,END);
  249.             else
  250.                 do_menu(EDIT,START);
  251.             break;
  252.         case 'N':        /* New file */
  253.             do_menu(FILE,NEW);
  254.             break;
  255.         case 'O':        /* Open file */
  256.             do_menu(FILE,OPEN);
  257.             break;
  258.         case 'P':
  259.             if ( (keymods & K_RSHIFT) || (keymods & K_LSHIFT) )
  260.                 do_menu(FILE,SETPRINT);
  261.             else
  262.                 do_menu(FILE,PRINT);
  263.             break;
  264.         case 'Q':
  265.             do_menu(FILE,QUIT);
  266.             break;
  267.         case 'S':
  268.             if ( (keymods & K_RSHIFT) || (keymods & K_LSHIFT) )
  269.                 do_menu(FILE,SAVEAS);
  270.             else
  271.                 do_menu(FILE,SAVE);
  272.             break;
  273.         case 'V':
  274.             do_menu(EDIT,PASTE);
  275.             break;
  276.         case 'W':
  277.             do_menu(EDIT,CLOSE);
  278.             break;
  279.         case 'X':
  280.             do_menu(EDIT,CUT);
  281.             break;
  282.     }
  283. }
  284.  
  285. /*
  286.     name: cur_up
  287.     utility:  
  288.     comment: 
  289.     parameters:
  290.     return:
  291.     date: 13 may 96
  292.     author: C.Moreau
  293.     modifications:
  294. */
  295. static void cur_up(windowptr thewin)
  296. {    
  297.     if(thewin->position - 16 >= 0)
  298.     {
  299.         thewin->position -= 16;
  300.         check_scroll(thewin);
  301.     }
  302. }
  303.  
  304. static void cur_down(windowptr    thewin)
  305. {
  306.     if(thewin->position + 16 <= thewin->flen-1)
  307.     {
  308.         thewin->position += 16;
  309.         check_scroll(thewin);
  310.     }
  311. }
  312.  
  313. static void cur_right(windowptr thewin)
  314. {
  315.     if (thewin->position + 1 < thewin->flen)
  316.     {
  317.         thewin->position++;
  318.         check_scroll(thewin);
  319.     }
  320. }
  321.  
  322. static void cur_left(windowptr thewin)
  323. {
  324.     if (thewin->position - 1 >= 0)
  325.     {
  326.         thewin->position--;
  327.         check_scroll(thewin);
  328.     }
  329. }
  330.  
  331. /*
  332.     name: pag_down
  333.     utility:  
  334.     comment: 
  335.     parameters:
  336.     return:
  337.     date: 1995
  338.     author: C.Moreau
  339.     modifications:
  340. */
  341. static void pag_down(windowptr thewin)
  342. {
  343.     const long winlines = thewin->work.g_h/gl_hchar; /* no of lines can put in window  */
  344.     
  345.     if (thewin->position+(winlines*16) < thewin->flen)
  346.     {
  347.         long topwlnmax, topwline;
  348.         const long totallines = 1 + thewin->flen/16;
  349.         
  350.         thewin->topchar += winlines * 16;
  351.         thewin->position += winlines * 16;            /* go down 1 page */
  352.             /* update slider pos */
  353.         topwlnmax = ((totallines - winlines)>0) ? (totallines-winlines) : 0;
  354.           topwline = (thewin->topchar/16 < topwlnmax) ? thewin->topchar/16 : topwlnmax;
  355.         thewin->vslidepos = (int)((1000*topwline)/topwlnmax);
  356.  
  357.         send_redraw(thewin);
  358.     }
  359.     else
  360.     {
  361.         thewin->position = thewin->flen-1;
  362.         check_scroll(thewin);
  363.     }
  364. }
  365.  
  366. /*
  367.     name: pag_up
  368.     utility:  
  369.     comment: 
  370.     parameters:
  371.     return:
  372.     date: 1995
  373.     author: C.Moreau
  374.     modifications:
  375. */
  376. static void pag_up(windowptr thewin)
  377. {
  378.     const long winlines = thewin->work.g_h/gl_hchar; /* no of lines can put in window  */
  379.  
  380.     if (thewin->position-(winlines*16) >= 0)
  381.     {
  382.         long topwlnmax, topwline;
  383.         const long totallines = 1 + thewin->flen/16;
  384.  
  385.         thewin->topchar -= winlines * 16;
  386.         thewin->position -= winlines * 16;            /* go down 1 page */
  387.             /* update slider pos */
  388.         topwlnmax = ((totallines - winlines)>0) ? (totallines-winlines) : 0;
  389.           topwline = (thewin->topchar/16 < topwlnmax) ? thewin->topchar/16 : topwlnmax;
  390.         thewin->vslidepos = (int)((1000*topwline)/topwlnmax);
  391.  
  392.         send_redraw(thewin);
  393.     }
  394.     else
  395.     {
  396.         thewin->position = 0;
  397.         check_scroll(thewin);
  398.     }
  399. }
  400.  
  401. static void pag_home(windowptr thewin)
  402. {
  403.     thewin->position = 0;
  404.     check_scroll(thewin);
  405. }
  406.  
  407. static void pag_end(windowptr thewin)
  408. {
  409.     thewin->position = thewin->flen-1;
  410.     check_scroll(thewin);
  411. }
  412.  
  413. /*
  414.     name: check_scroll
  415.     utility: cursor off screen?? scroll or reposition 
  416.     comment: 
  417.     parameters:
  418.     return:
  419.     date: 1989
  420.     author: Jim Charlton
  421.     modifications:
  422.         1995: C.Moreau:
  423. */
  424. void check_scroll(windowptr    thewin)
  425. {
  426.     const long winlines = thewin->work.g_h/gl_hchar; /* no of lines can put in window  */
  427.     const long bottomchar = thewin->topchar + (winlines * 16) - 1;
  428.     
  429.     if (thewin->position > bottomchar)        /* cursor down actual page */
  430.     {
  431.         if (thewin->position > bottomchar + 16)    /* more than 1 line down */
  432.         {
  433.             jump_pos(thewin);
  434.             send_redraw(thewin);
  435.         }
  436.         else
  437.         {
  438.             send_arrow(thewin,WA_DNLINE);
  439.         }
  440.     }
  441.     else if (thewin->position < thewin->topchar)    /* cursor up actual page */
  442.     {
  443.         if (thewin->position < thewin->topchar - 16) /* more than 1 line up */
  444.         {
  445.             jump_pos(thewin);
  446.             send_redraw(thewin);
  447.         }
  448.         else
  449.         {
  450.             send_arrow(thewin,WA_UPLINE);
  451.         }
  452.     }
  453. }
  454.  
  455. /*
  456.     name: repos
  457.     utility: reposition and redraw with cursor on screen 
  458.     comment: 
  459.     parameters:
  460.     return:
  461.     date: 1989
  462.     author: Jim Charlton
  463.     modifications:
  464.         1995: C.Moreau:
  465. */
  466. int repos(windowptr    thewin)
  467. {
  468.     const long winlines = thewin->work.g_h/gl_hchar; /* no of lines can pu */ 
  469.     const long bottomchar = thewin->topchar + (winlines * 16) - 1;
  470.     
  471.     if ( (thewin->position > bottomchar )||
  472.             (thewin->position < thewin->topchar) )
  473.     {
  474.         jump_pos(thewin);
  475.         return(1);
  476.     }
  477.     else
  478.         return(0);
  479. }
  480.  
  481. /*
  482.     name: jump_pos
  483.     utility: reset display parameters so cursor on screen  
  484.     comment: 
  485.     parameters:
  486.     return:
  487.     date: 1989
  488.     author: Jim Charlton
  489.     modifications:
  490.         1995: C.Moreau:
  491. */
  492. void jump_pos(windowptr    thewin)
  493. {    
  494.     long topwlnmax, topwline;
  495.     int    vslide;
  496.     const long totallines = 1 + thewin->flen/16;
  497.     const long winlines = thewin->work.g_h/gl_hchar; /* no of lines can put in window  */
  498.  
  499.     thewin->topchar = ((thewin->position - (8 * winlines)) & 0xfffffff0L);
  500.     if (thewin->topchar < 0)
  501.         thewin->topchar = 0;
  502.  
  503.     topwlnmax = ((totallines - winlines)>0) ? (totallines-winlines) : 0;
  504.       topwline = (thewin->topchar/16 < topwlnmax) ? thewin->topchar/16 : topwlnmax;
  505.     vslide = (int)((1000*topwline)/topwlnmax);
  506.     thewin->vslidepos = vslide;
  507. }
  508.  
  509. /*
  510.     name: ins_rpl
  511.     utility: enters hex charactes into the file   
  512.     comment: 
  513.     parameters:
  514.     return:
  515.     date: 1989
  516.     author: Jim Charlton
  517.     modifications:
  518.         1995: C.Moreau:
  519. */
  520. void ins_rpl(windowptr    thewin) 
  521. {
  522.     const int x = thewin->xcur;
  523.     const int y = thewin->ycur + gl_hchar-1;
  524.     const int handle = thewin->graf.handle;
  525.     char    *inbuf;
  526.     long    lnum;
  527.     unsigned    b;
  528.  
  529.     inbuf = thewin->input;
  530.     inbuf[thewin->icount]='\0';
  531.  
  532.     if( (thewin->position==thewin->flen-1) && !ins) /* in replace mode on terminal dummy byte */
  533.     {
  534.         rsc_alert(LAST_BYTE2);                
  535.         thewin->icount = 0;
  536.     }             
  537.     else if (hex_chk(thewin,inbuf[thewin->icount-1])==1) /* is a hex char */
  538.     {
  539.             if (thewin->icount==1) /* first hex char coming   */
  540.             {
  541.                 b = (int)saveb;    /* saveb is the current byte    */
  542.                 b = b & 0xf;
  543.                 lnum = strtol(thewin->input,(char **)NULL,16);
  544.                 lnum = (lnum<<4) + b;
  545.                 /* write it back with top nibble changed    */
  546.                 putbyte(thewin,thewin->position,lnum);
  547.                 v_gtext(handle, x, y, inbuf);
  548.             }
  549.             else     
  550.     /* two valid hex characters entered. Enter in buffer and redrw line */
  551.             {
  552.                 lnum = strtol(thewin->input,(char **)NULL,16);
  553.                 enter_it(thewin,inbuf,lnum);
  554.             }
  555.     }    
  556. }
  557.  
  558. /*
  559.     name: enter_it
  560.     utility: insert or replace byte redrawing screen  
  561.     comment: 
  562.     parameters:
  563.     return:
  564.     date: 1989
  565.     author: Jim Charlton
  566.     modifications:
  567.         1995: C.Moreau:
  568. */
  569. void enter_it(windowptr    thewin,char    *inbuf,long    lnum)
  570. {
  571.     const int handle = thewin->graf.handle;
  572.     const int x = thewin->xcur;
  573.     const int y = thewin->ycur + gl_hchar-1;
  574.  
  575.     v_gtext(handle, x, y, inbuf);
  576.     thewin->icount=0;
  577.  
  578.     if(!ins)    /* in replace mode    */
  579.     {
  580.         putbyte(thewin,thewin->position,lnum);
  581.         graf_mouse(M_OFF, 0L);
  582.         one_line2(thewin,thewin->position);    /* see onepage.c */
  583.         cur_right(thewin);
  584.         graf_mouse(M_ON, 0L);
  585.     }
  586.     else    /* in insert mode    */
  587.     {
  588.         inbuf = "  ";
  589.         *inbuf = (char)lnum;
  590.             /* restore current byte as HEX input dirties it */
  591.         putbyte(thewin,thewin->position,saveb);
  592.         insert_it(thewin,1L,inbuf);  /* see edit.c    */
  593.         graf_mouse(M_OFF, 0L);
  594.         if(thewin->position == thewin->flen-1)
  595.             one_line2(thewin,thewin->position);
  596.         else
  597.         {    
  598.             one_line2(thewin,thewin->position);    /* see onepage.c */
  599.             partial = TRUE;
  600.             send_redraw(thewin);
  601.         }
  602.         graf_mouse(M_ON, 0L);
  603.         cur_right(thewin);
  604.     }
  605. }
  606.  
  607. /*
  608.     name: hex_chk
  609.     utility: checks to see if a hex character was entered  
  610.     comment: 
  611.     parameters:
  612.     return:
  613.     date: 1989
  614.     author: Jim Charlton
  615.     modifications:
  616.         1995: C.Moreau:
  617. */
  618. static int hex_chk(windowptr    thewin, char    ch)
  619. {
  620.     int ret;
  621.     static int times;
  622.  
  623.     switch (ch)
  624.     {
  625.         case '0': 
  626.         case '1': 
  627.         case '2': 
  628.         case '3': 
  629.         case '4': 
  630.         case '5':
  631.         case '6': 
  632.         case '7': 
  633.         case '8': 
  634.         case '9': 
  635.         case 'A': 
  636.         case 'B':
  637.         case 'C': 
  638.         case 'D': 
  639.         case 'E': 
  640.         case 'F':
  641.         case 'a': 
  642.         case 'b':
  643.         case 'c': 
  644.         case 'd': 
  645.         case 'e': 
  646.         case 'f':     ret = 1 ; times = 0; break;
  647.         case  8 :     thewin->icount = 0;  /* backspace  after one hex char */
  648.                     graf_mouse(M_OFF, 0L);
  649.                             /*    restore the byte and redraw line    */
  650.                     putbyte(thewin,thewin->position,saveb);
  651.                     one_line2(thewin,thewin->position);
  652.                     graf_mouse(M_ON, 0L);
  653.                     times = 0;
  654.                     ret = 0;
  655.                       break;
  656.         default :    Bconout(2,7);
  657.                     thewin->icount--; /*  wipe out nonhex char  */
  658.                     times++;
  659.                     if (times>2)
  660.                         rsc_alert(ENTER_HEX);                
  661.                     ret = 0;
  662.     }
  663.     return(ret);
  664. }
  665.  
  666. /*
  667.     name: init_keys
  668.     utility: initialise keys.c module  
  669.     comment: 
  670.     parameters:
  671.     return:
  672.     date: 10 may 96
  673.     author: C.Moreau
  674.     modifications:
  675. */
  676. void init_keys(void)
  677. {
  678.     kt = Keytbl((void *)-1, (void *)-1, (void *)-1);
  679. }
  680.